//
// Created by zuo on 2024/6/30.
// 题目链接 https://www.lanqiao.cn/problems/741/learning/?page=1&first_category_id=1&problem_id=741
//

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    // 取消同步流  加快速度
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n;
    cin >> n;
    // 优先队列
    priority_queue<ll, vector<ll>, greater<ll>> pq;
    for (int i = 0; i < n; i++)
    {
        ll x;
        cin >> x;
        pq.push(x);
    }

    ll sum = 0;
    while (pq.size() >= 2)
    {
        ll x = pq.top();
        pq.pop(); // 最小值
        ll y = pq.top();
        pq.pop(); // 最小值弹出之后的最小值

        sum += x + y;
        pq.push(x + y);
    }
    cout << sum << '\n';

    return 0;
}
